package com.czh.springboot04.FileController;

import com.czh.springboot04.util.OSSUtil;
import com.czh.springboot04.util.R;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileController {
    private String rootPath = "D:/upload";


    @PostMapping("/upload")
    public R fileUpload(MultipartFile file) throws IOException {
        if(file.isEmpty()){
            return R.fail("失败");
        }
        String fileName = file.getOriginalFilename();
        fileName = UUID.randomUUID().toString().replace("-","")+ "-" + fileName;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");
        String dirPath = dateFormat.format(new Date());
        File dfile = new File(rootPath + File.separator + dirPath);
        if(!dfile.exists()){
            dfile.mkdirs(); //创建文件夹
        }
        file.transferTo(new File(dfile,fileName));
        String finalPath = dirPath + "/" + fileName;
        System.out.println("最终路径："+finalPath);
        return R.ok(finalPath);
    }


    @PostMapping("/uploadOss")
    public R fileUploadOss(MultipartFile file) throws IOException {
        if(file.isEmpty()){
            return R.fail("失败");
        }
        String fileName = file.getOriginalFilename();
        fileName = UUID.randomUUID().toString().replace("-","")+ "-" + fileName;
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");
//        String dirPath = dateFormat.format(new Date());
//        File dfile = new File(rootPath + File.separator + dirPath);
//        if(!dfile.exists()){
//            dfile.mkdirs(); //创建文件夹
//        }
        String furl = OSSUtil.upload(fileName, file.getInputStream());
// 最终会把上传后的图片的全路径直接放入到数据库表中即可
        System.out.println("图片完整的网址是："+ furl);
        return R.ok(furl);
    }

    @RequestMapping("/downLoad")
    public void downLoad(String fileName, HttpServletResponse response) throws IOException {
        File file = new File(rootPath, fileName);
        System.out.println(fileName);
        FileInputStream fin = new FileInputStream(file);
        response.setHeader("Content-Disposition","filename="+fileName);
        ServletOutputStream out = response.getOutputStream();
        IOUtils.copy(fin,out);
        out.close();
        fin.close();
    }

    @DeleteMapping("/delete")
    public R delete(@RequestParam String fileName){
        File file = new File(rootPath, fileName);
        // 检查文件是否存在
        if (file.exists()) {
            // 尝试删除文件
            if (file.delete()) {
                System.out.println("文件删除成功：" + fileName);
            } else {
                System.out.println("文件删除失败：" + fileName);
            }
        } else {
            System.out.println("文件不存在：" + fileName);
        }
        return R.ok("成功");
    }
}
